<template>
	<view class="container">
		<view class="wrapper">
			<view class="left-top-sign">FIND</view>
			<view class="welcome">
				找回密码！
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">手机号码</text>
					<input 
						type="number" 
						:value="mobile" 
						placeholder="请输入手机号码"
						maxlength="11"
						data-key="mobile"
						@input="inputChange"
					/>
				</view>
				<view class="input-item smscode">
					<text class="tit">手机验证码</text>
					<input type="text" :value="smscode" placeholder="请输入验证码" maxlength="6" data-key="smscode"
						@input="inputChange" />
					<button class="b-btn" @click="sendsmscode" :disabled="sendsmsing">{{sendtext}}</button>
				</view>
				<view class="input-item">
					<text class="tit">密码</text>
					<input type="text" value="" placeholder="6-18位不含特殊字符" placeholder-class="input-empty"
						maxlength="18" password data-key="password" @input="inputChange" />
				</view>
				<view class="input-item">
					<text class="tit">确认密码</text>
					<input type="text" value="" placeholder="6-18位不含特殊字符" placeholder-class="input-empty"
						maxlength="18" password data-key="confirmpassword" @input="inputChange" />
				</view>
			</view>
			<button class="confirm-btn" @click="toEdit" :disabled="editing">确认修改</button>
		</view>
		<view class="register-section">
			已有账号?
			<text @click="$api.toUrl(`/pages/public/login`)">去登录</text>
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex';
	
	export default{
		data(){
			return {
				mobile: '',
				smscode: '',
				password:'',
				confirmpassword:'',
				sendsmsing: false,
				sendtext: '发送验证码',
				editing: false
			}
		},
		onLoad(){
			
		},
		methods: {
			...mapMutations(['login']),
			
			inputChange(e){
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack(){
				uni.navigateBack();
			},
			//发送手机验证码
			sendsmscode() {
				this.sendsmsing = true;
				const {
					mobile
				} = this;
				
				/* 数据验证模块*/
				if (!/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(mobile)) {
					this.$helper.toast('注册手机号不合法');
					this.sendsmsing = false;
					return;
				}
				
				const param = {};
				param.mobile = mobile;
				param.type = 'password';
				this.$http.request('Login', 'sendsmscode', 'POST', param, res => {
					if (res.status == 1) {
						if (res.data.error == 0) {
							// '发送成功';
							this.countDown();
						} else {
							this.sendsmsing = false;
						}
					}
				});
			},
			countDown(){
				// 初始化定时器
				let t = null;
				// 倒计时1分钟
				let timer = 60;
				// 计时器清零
				clearInterval(t);
				// 按钮显示时间
				this.sendtext = `${timer} s`;
				t = setInterval(() => {
				// 当倒计时完时，显示重新发送,按钮开启
					if (timer == 0) {
						clearInterval(t);
						this.sendtext = "重新发送";
						this.sendsmsing = false;
						return;
					}
					// 实现倒计时效果
					timer--;
					this.sendtext = `${timer} s`;
				}, 1000);
			},
			// 点击修改
			toEdit(){
				this.editing = true;
				const { mobile, smscode, password, confirmpassword } = this;
				/* 数据验证模块*/
				if (!/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(mobile)) {
					this.$helper.toast('注册手机号不合法');
					this.editing = false;
					return;
				}
				if (!smscode) {
					this.$helper.toast('验证码不能为空');
					this.editing = false;
					return;
				}
				
				let regex = /([0-9A-Za-z]{6,18}$)/;
				if (!regex.test(password)) {
					this.$helper.toast('请输入6-18位不含特殊字符');
					this.logining = false;
					return;
				}
				
				if (password != confirmpassword) {
					this.$helper.toast('两次密码不一致');
					this.editing = false;
					return;
				}
				
				const param = {};
				param.mobile = mobile;
				param.smscode = smscode;
				param.password = password;
				param.password2 = confirmpassword;
				this.$http.request('Login', 'passwordedit', 'POST', param, res => {
					console.log('Login/passwordedit', res);
					this.editing = false;
					if (res.status == 1) {
						if (res.data.error == 0) {
							// '修改成功';
							this.$helper.toast('密码修改成功');
							uni.navigateTo({url:'/pages/public/login'});
						}
					}
				});
			},
			
			
		},

	}
</script>

<style lang='scss'>
	page{
		background: #fff;
	}
	.container{
		padding-top: 115px;
		position:relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}
	.wrapper{
		position:relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}
	.back-btn{
		position:absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}
	.left-top-sign{
		font-size: 120upx;
		color: $page-color-base;
		position:relative;
		left: -16upx;
	}
	.right-top-sign{
		position:absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;
		&:before, &:after{
			display:block;
			content:"";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}
		&:before{
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}
		&:after{
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}
	.left-bottom-sign{
		position:absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}
	.welcome{
		position:relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0,0,0,.3);
	}
	.input-content{
		padding: 0 60upx;
	}
	.input-item{
		display:flex;
		flex-direction: column;
		align-items:flex-start;
		justify-content: center;
		padding: 0 30upx;
		background:$page-color-light;
		height: 100upx;
		border-radius: 4px;
		margin-bottom: 30upx;
		&:last-child{
			margin-bottom: 0;
		}
		.tit{
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}
		input{
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}	
	}
	.smscode{
		position:relative;
	}
	.b-btn {
		position: absolute;
		right: 0upx;
		top: 10upx;
		width: 200upx;
		height: 80upx;
		text-align: center;
		line-height: 80upx;
		font-size: 26upx;
		color: #000000;
		border-radius: 10px;
		background: linear-gradient(left, #ffd465, #ff0000);
		z-index: 1;
	}
	.confirm-btn{
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;
		&:after{
			border-radius: 100px;
		}
	}
	.forget-section{
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}
	.register-section{
		position:fixed;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;
		text{
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>
